package com.weaver.qfengx.proxy.workflow;

import com.engine.core.interceptor.AbstractCommandProxy;
import com.engine.core.interceptor.Command;
import com.engine.workflow.cmd.requestForm.RequestSubmitCmd;
import com.weaver.qfengx.*;
import weaver.hrm.User;

import java.util.Map;

/**
 * @Description
 * @Author Qfengx <qffg1998@126.com>
 * @Date 2021-08-09 14:51:08
 */
public abstract class BaseWorklowProxyCmd extends AbstractCommandProxy<Map<String, Object>> {

    protected LogUtils log = new LogUtils(this.getClass());
    protected String proxyName = "封装后的流程节点Action基础类";
    public void setProxyName(String proxyName) {
        this.proxyName = proxyName;
    }
    protected void setLog(Class clazz) {
        this.log = new LogUtils(clazz);
    }


    protected Map<String, String> mainTableMap;
    protected String workflowid = "";
    protected String requestid = "";
    protected String creatorname = "";
    protected String creatorid = "";
    protected String formid = "";
    protected String maintable = "";
    protected String requestname = "";
    protected String operatorid = "";
    protected String operatorname = "";



    public abstract Map<String, Object> execCode(RequestSubmitCmd requestSubmitCmd) throws Exception;

    @Override
    public Map<String, Object> execute(Command<Map<String, Object>> targetCommand) {
        try {
            log.writeLog("=========开始执行代理方法:" + proxyName +" ProxyCmd==============> " + DateUtils.datetime());
            initWorkflowParam((RequestSubmitCmd) targetCommand);
            long startExecCode = System.currentTimeMillis();
            log.writeLog("开始执行业务逻辑代码 => " + startExecCode);
            Map<String, Object> res =  execCode((RequestSubmitCmd) targetCommand);
            log.writeLog("结束执行业务逻辑代码 => cost: " + (System.currentTimeMillis() - startExecCode) + ", " + DateUtils.datetime());
            log.writeLog("=========代理方法执行结束" + proxyName + " ProxyCmd==============> " + DateUtils.datetime());
            return res;
        } catch (Exception e) {
            e.printStackTrace();
            log.writeLog(proxyName + "代理方法执行异常 => " + e.getMessage());
            return nextExecute(targetCommand);
        }
    }

    private void initWorkflowParam(RequestSubmitCmd requestSubmitCmd) { //初始化成员属性
        Map<String, Object> params = requestSubmitCmd.getParams();
        requestid = StringUtils.val(params.get("requestid"));
        workflowid = StringUtils.val(params.get("workflowid"));
        creatorid = StringUtils.val(params.get("creater"));
        User user = User.getUser(NumberUtils.parseInt(creatorid), 0);
        creatorname = user == null ? null : user.getLastname();
        formid = StringUtils.val(params.get("formid"));
        maintable = "formtable_main_" + (-NumberUtils.parseInt(formid));
        mainTableMap = WorkflowUtils.data(requestid, workflowid);
        requestname = DaoUtils.querySingleVal("select requestname from workflow_requestbase where requestid = ?", requestid);
        User optUser = requestSubmitCmd.getUser(); // 当前操作人员
        operatorid = StringUtils.val(optUser.getUID());
        operatorname = optUser.getLastname();
    }
}
